var e = function(e) {
    return e && e.__esModule ? e : {
        default: e
    };
}(require("../..//utils/toptip.js")), t = function(e) {
    if (e && e.__esModule) return e;
    var t = {};
    if (null != e) for (var a in e) Object.prototype.hasOwnProperty.call(e, a) && (t[a] = e[a]);
    return t.default = e, t;
}(require("./card-scanner")), a = [ "name", "tel_cell", "company", "title", "email" ], i = {
    addr: "地址",
    company: "公司",
    department: "部门",
    tel_work: "电话",
    tel_cell: "手机",
    title: "职务",
    email: "邮箱"
};

Component({
    data: {
        form: {
            isclaim: 0
        },
        remarkHint: "请输入更多名片信息",
        searchListDisplay: 4
    },
    ready: function() {
        this.createForm = this.selectComponent("#createForm");
    },
    methods: {
        setScanForm: function(e) {
            this.loadRecognizeCard(e.data);
        },
        recognizeCardView: function(e) {
            this.triggerEvent("file", {
                value: e
            }, {}), t.ocr(e).then(this.loadRecognizeCard.bind(this));
        },
        loadRecognizeCard: function(e) {
            var t = e.name, a = e.tel_cell, i = e.company, n = e.title, r = e.email, o = a[0] ? a[0].replace(/^\+?86/, "") : "";
            this.setData({
                form: {
                    realname: t[0],
                    mobile: o,
                    corpname: i[0],
                    position: n[0],
                    email: r[0],
                    remark: this.fillRemark(e)
                }
            }), this.triggerEvent("input", {
                name: "mobile",
                value: o
            }, {}), wx.hideLoading();
        },
        fillRemark: function(e) {
            var t = [];
            return Object.keys(e).forEach(function(n) {
                var r = e[n];
                if (Array.isArray(r)) {
                    var o = (a.includes(n) ? r.slice(1) : r).join(", ");
                    o && t.push(i[n] + ": " + o);
                }
            }), t.join("\n");
        },
        onInput: function(e) {
            this.triggerEvent("input", e.detail, {});
        },
        onFail: function(t) {
            var a = t.detail.value;
            e.default.show(this, a);
        },
        submit: function(e) {
            var t = this;
            this.createForm.getFormData() && wx.downloadFile({
                url: "https://img.ibos.cn/common/default_avatar.png",
                complete: function(e) {
                    t.trigger("submit", t.getConf(e.tempFilePath));
                }
            });
        },
        getConf: function(e) {
            var t = {}, a = this.data.form;
            return e ? Object.assign(t, {
                formData: a,
                filePath: e,
                name: "avatar"
            }) : (a.avatar = "https://img.ibos.cn/common/default_avatar.png", t.data = a), t;
        },
        takeShot: function() {
            var e = this;
            wx.showActionSheet({
                itemList: [ "拍照识别", "从相册中选择" ],
                success: function(t) {
                    var a = t.tapIndex;
                    0 === a ? wx.canIUse && wx.canIUse("camera") ? wx.redirectTo({
                        url: "/public/pages/card-scanner/card-scanner"
                    }) : e.chooseImage("camera") : 1 === a && e.chooseImage("album");
                }
            });
        },
        chooseImage: function(e) {
            t.chooseImage(e).then(this.recognizeCardView.bind(this));
        }
    }
});